﻿using Grasshopper.GUI;
using Grasshopper.GUI.Canvas;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Attributes;
using System;
using System.Drawing;

namespace AiMoInGH.Component
{
    class AiMoInGHComponentAttributes : GH_ComponentAttributes
    {
        readonly AiMoInGHComponent owner;
        public AiMoInGHComponentAttributes(AiMoInGHComponent component) : base(component)
        {
            owner = component; ;
            versionFont = new Font(GH_FontServer.StandardAdjusted.FontFamily, 2f);
            versionBrush = new SolidBrush(Color.FromArgb(120, Color.White));
        }
        public override GH_ObjectResponse RespondToMouseDoubleClick(GH_Canvas sender, GH_CanvasMouseEvent e)
        {
            if (Bounds.Contains(e.CanvasLocation))
            {
                owner.OpenAiMoScriptsWindow();
                return GH_ObjectResponse.Release;
            }
            return base.RespondToMouseDoubleClick(sender, e);
        }
        public override bool IsPickRegion(PointF point)
        {
            return owner.ComponentAdditionalIsPickRegion(point) || base.IsPickRegion(point);
        }
        public override GH_ObjectResponse RespondToMouseDown(GH_Canvas sender, GH_CanvasMouseEvent e)
        {
            return owner.ComponentAdditionalRespondToMouseDown(sender, e);
        }
        public GH_ObjectResponse RespondToMouseDownBase(GH_Canvas sender, GH_CanvasMouseEvent e)
        {
            return base.RespondToMouseDown(sender, e);
        }

        public override GH_ObjectResponse RespondToMouseMove(GH_Canvas sender, GH_CanvasMouseEvent e)
        {
            return owner.ComponentAdditionalRespondToMouseMove(sender, e);
        }
        public GH_ObjectResponse RespondToMouseMoveBase(GH_Canvas sender, GH_CanvasMouseEvent e)
        {
            return base.RespondToMouseMove(sender, e);
        }

        public override GH_ObjectResponse RespondToMouseUp(GH_Canvas sender, GH_CanvasMouseEvent e)
        {
            return owner.ComponentAdditionalRespondToMouseUp(sender, e);
        }
        public GH_ObjectResponse RespondToMouseUpBase(GH_Canvas sender, GH_CanvasMouseEvent e)
        {
            return base.RespondToMouseUp(sender, e);
        }
        protected override void Layout()
        {
            Pivot = GH_Convert.ToPoint(Pivot);
            m_innerBounds = LayoutComponentBox(base.Owner);

            int minLeft = 120;
            int num = 0;
            foreach (IGH_Param item in owner.Params.Input)
            {
                num = Math.Max(num, GH_FontServer.StringWidth(item.NickName, GH_FontServer.StandardAdjusted));
            }
            foreach (IGH_Param item in owner.Params.Output)
            {
                num = Math.Max(num, GH_FontServer.StringWidth(item.NickName, GH_FontServer.StandardAdjusted));
            }
            num = Math.Max(num + 6, 12);
            if (num < minLeft)
            {
                int dis = minLeft - num;
                m_innerBounds.Width += dis;
                m_innerBounds.X -= dis;
            }
            LayoutInputParams(base.Owner, m_innerBounds);
            LayoutOutputParams(base.Owner, m_innerBounds);
            RectangleF newBounds = LayoutBounds(base.Owner, m_innerBounds);
            versionLocation = new PointF(newBounds.X + 5, newBounds.Bottom - 4);
            newBounds.Height += 3;
            Bounds = newBounds;
            owner.ComponentAdditionalLayout(Bounds);
        }
        Font versionFont;
        PointF versionLocation;
        Brush versionBrush;
        protected override void Render(GH_Canvas canvas, Graphics graphics, GH_CanvasChannel channel)
        {
            base.Render(canvas, graphics, channel);
            if (channel == GH_CanvasChannel.Objects)
            {
                if (GH_Canvas.ZoomFadeHigh != 0)
                {
                    graphics.DrawString($"AiMoGH {Version.version}", versionFont, versionBrush, versionLocation);
                }
                owner.ComponentAdditionalRender(canvas, graphics);
            }
        }
    }
}